package com.example.demo.service.impl;

import com.example.demo.entity.Book;
import com.example.demo.entity.Borrow;
import com.example.demo.mapper.BookMapper;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Override
    public List<Borrow> getBorrowList() {
        return bookMapper.getBorrowList();
    }

    @Override
    public Map<Book, Boolean> getBookList() {
        Set<Integer> set = new HashSet<>();
        this.getBorrowList().forEach(borrow -> set.add(borrow.getBid()));
        Map<Book, Boolean> map = new LinkedHashMap<>();
        bookMapper.getBookList().forEach(book -> map.put(book, set.contains(book.getId())));
        return map;
    }

    @Override
    public List<Book> getActiveBookList() {
        Set<Integer> set = new HashSet<>();
        this.getBorrowList().forEach(borrow -> {
            set.add(borrow.getBid());
        });
        return bookMapper.getBookList()
                .stream()
                .filter(book -> !set.contains(book.getId()))
                .toList();
    }

    @Override
    public Boolean addBorrow(int sid, int bid) {
        return bookMapper.addBorrow(sid, bid);
    }

    @Override
    public Boolean returnBorrow(int id) {
        return bookMapper.deleteBorrow(id);
    }

    @Override
    public Boolean addBook(String title, String desc, double price) {
        return bookMapper.addBook(title, desc, price);
    }

    @Override
    public Boolean deleteBook(int bid) {
        return bookMapper.deleteBook(bid);
    }
}
